<script setup lang="ts">
import { Form } from '@/components/Form'
import { useForm } from '@/hooks/web/useForm'
import { PropType, reactive, watch, ref } from 'vue'
// 获取字典
import { useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache()
/****************************** 变量 start ******************************/
// const dict: any = wsCache.get('dict')
// 获取字典
const userInfo: any = wsCache.get('userInfo')
const data = ref<any>({})

// 获取字典
// import { useCache } from '@/hooks/web/useCache'
// const { wsCache } = useCache()
// /****************************** 变量 start ******************************/
// const dict: any = wsCache.get('dict')
const props = defineProps({
  addFolder: {
    type: Object as PropType<Nullable<any>>,
    default: () => null
  }
})
const schema: any = [
  {
    field: 'customer_name',
    component: 'Input',
    label: '所属牧场',
    componentProps: {
      readonly: true
    }
  },

  {
    field: 'folder_name',
    component: 'Input',
    label: '文件夹名称'
  },
  {
    field: 'belong_type',
    label: '所属类型',
    component: 'Select',
    componentProps: {
      options: [
        {
          label: '个人',
          value: '1'
        },
        {
          label: '组织',
          value: '2'
        }
      ]
    }
  }
]
const rules = reactive({})

const { register, methods, elFormRef } = useForm({
  schema
})

watch(
  () => props.addFolder,
  (currentRow) => {
    console.log(currentRow, 'currentRow')
    if (!currentRow) return
    currentRow.customer_name = userInfo.default_org_name
    currentRow.customer_id = userInfo.default_org_id
    const { setValues } = methods
    if (props.addFolder && props.addFolder.id) {
      data.value = {
        id: 1,
        creation_user_id: '2',
        creation_user_name: '张三',
        customer_id: '1',
        customer_name: 'AAA牧场',
        time: '2022-03-02 11:10:00',
        num: 2,
        belong_type: 1
      }
    }
    setValues(currentRow)
  },
  {
    deep: true,
    immediate: true
  }
)

defineExpose({
  elFormRef,
  getFormData: methods.getFormData
})
</script>

<template>
  <Form :rules="rules" @register="register" :data="data" />
</template>
